<template>
  <el-table :data="depts" style="width: 100%">
    <el-table-column label="部门编号" prop="deptno" />
    <el-table-column label="部门名称" prop="dname" />
    <el-table-column label="部门所在地" prop="loc" />

    <el-table-column align="right">

      <template #header>
        <el-input v-model="search" size="small" placeholder="回车搜索" @keyup.enter="select" />
      </template>

      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[2, 5]" :small="small"
                 :disabled="disabled" :background="background" layout="total, sizes, prev, pager, next, jumper" :total="total"
                 @size-change="handleSizeChange" @current-change="handleCurrentChange" />

  <el-button type="primary" @click="handleAdd">添加</el-button>

  <el-dialog v-model="centerDialogVisible" title="修改" width="30%" center>
    <el-form ref="ruleFormRef" :model="editDept" label-width="120px" class="demo-ruleForm">
      <el-form-item label="部门编号" prop="deptno">
        <el-input v-model="editDept.deptno" type="text" autocomplete="off" disabled />
      </el-form-item>
      <el-form-item label="部门名称" prop="dname">
        <el-input v-model="editDept.dname" type="text" autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门所在地" prop="loc">
        <el-input v-model="editDept.loc" type="text" autocomplete="off" />
      </el-form-item>
    </el-form>

    <!-- 修改 -->
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="editDeptById">
          修改
        </el-button>
      </span>
    </template>
  </el-dialog>
  <el-dialog v-model="addDialogVisible" title="添加" width="30%" center>
    <el-form ref="ruleFormRef" :model="addDept" label-width="120px" class="demo-ruleForm">
      <el-form-item label="部门编号" prop="deptno">
        <el-input v-model="addDept.deptno" type="text" autocomplete="off"  />
      </el-form-item>
      <el-form-item label="部门名称" prop="dname">
        <el-input v-model="addDept.dname" type="text" autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门所在地" prop="loc">
        <el-input v-model="addDept.loc" type="text" autocomplete="off" />
      </el-form-item>
    </el-form>

    <!-- 添加 -->
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addDeptById">
          添加
        </el-button>
      </span>
    </template>
  </el-dialog>

</template>
<script setup>
import axios from 'axios';
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
// 搜索
let search=ref('')

//添加
// 控制添加对话框
let addDialogVisible = ref(false)
// 用来记录需要添加的数据
let addDept = ref({})
// 添加按钮
let handleAdd = function () {
  // 显示添加对话框
  addDialogVisible.value = true
}

function addDeptById() {
  addDialogVisible.value = false
  axios.post('http://localhost:9090/test/dept/addDept', addDept.value).then((res) => {
    console.log(res)
    select()
    if (res.data == 1) {
      ElMessage({
        message: '添加成功.',
        type: 'success',
      })
      select()
    } else {
      ElMessage({
        message: '添加失败.',
        type: 'error',
      })
    }
  }).catch((err) => {
    console.log(err)
  })
}

// 用来控制修改的对话框是显示还是隐藏
let centerDialogVisible = ref(false)
// 用来记录需要修改的数据
let editDept = ref({})
// 修改按钮
const handleEdit = (index, row) => {
  // 显示修改对话框
  centerDialogVisible.value = true
  console.log(index, row)
  editDept.value = row
}

function editDeptById() {
  centerDialogVisible.value = false
  console.log(editDept.value)
  axios.post('http://localhost:9090/test/dept/update', editDept.value).then((res) => {
    console.log(res)
    if (res.data == 1) {
      ElMessage({
        message: '修改成功.',
        type: 'success',
      })
      select()
    } else {
      ElMessage({
        message: '修改失败.',
        type: 'error',
      })
    }
  }).catch((err) => {
    console.log(err)
  })
}

let depts = ref([])//用来存储所有员工信息
// 分页
let total = ref(0)
// 当前页码
let currentPage4 = ref(1)
// 每页数据条数
let pageSize4 = ref(5)
// 分页组件的样式
let small = ref(false)
let background = ref(false)
let disabled = ref(false)
// 每页数据条数发生变化时被调用
let handleSizeChange = function (val) {
  console.log(val)
  pageSize4.value = val
  select()
}
// 页码发生变化时被调用
let handleCurrentChange = function (val) {
  console.log(val)
  currentPage4.value = val
  select()
}

function select() {
  axios.get('http://localhost:9090/test/dept/getdept?pageNum=' + currentPage4.value + '&pageSize=' + pageSize4.value +'&dname='+search.value).then((res) => {
    console.log(res.data)
    depts.value = res.data.deptList
    total.value = res.data.total
  }).catch((err) => {
    console.log(err)
  })
}
select()

let handleDelete = function (index, row) {
  console.log(row.deptno)
  ElMessageBox.confirm(
      '确定要删除这条数据吗?',
      '删除功能',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        //将值传到后台
        //删除起始位置
        axios.get('http://localhost:9090/test/dept/delete?deptno=' + row.deptno).then((res) => {
          console.log(res)
          if (res.data == 1) {
            ElMessage({
              message: '删除成功！',
              type: 'success'
            })
            select()
          } else {
            ElMessage({
              message: '删除失败！',
              type: 'error'
            })
            select()
          }
        }).catch((err) => {
          console.log(err)
        })
        //删除结束位置
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })

}

</script>